package com.hoku.auth.service.security;

import com.hoku.auth.dao.IClientDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

/**
 * 客户端(其他模块微服务或者第三方可信服务)认证服务类，验证客户端是否有效
 * @author ZHUFEIFEI
 */
@Slf4j
public class BaseClientDetailService implements ClientDetailsService {

    @Autowired
    private IClientDao dao;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        log.info("client ===> {}", clientId);
        BaseClientDetails client = this.dao.getByClientId(clientId);
        if (client == null) {
            throw new NoSuchClientException("No client exists with clientId : " + clientId);
        }
        return client;
    }
}
